package com.erhuogo.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.bean.Response;
import com.erhuogo.entity.School;
import com.erhuogo.enumerate.Result;
import com.erhuogo.service.SchoolService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 高校Action
 * @author aibinxiao
 * @date 2016年10月26日 上午11:35:26
 */
public class SchoolAction extends ActionSupport {
	private static final long serialVersionUID = -46718224448287841L;
	private static Logger logger = LoggerFactory.getLogger(SchoolAction.class);
	
	// 模型驱动使用的对象
	private School school;

	public School getSchool() {
		return school;
	}

	public void setSchool(School school) {
		this.school = school;
	}
	
	// 注入SchoolService
	private SchoolService schoolService;

	public SchoolService getSchoolService() {
		return schoolService;
	}

	public void setSchoolService(SchoolService schoolService) {
		this.schoolService = schoolService;
	}
	
	// 根据城市Id获取到所有的高校
	private Integer cityId;
	
	public Integer getCityId() {
		return cityId;
	}

	public void setCityId(Integer cityId) {
		this.cityId = cityId;
	}

	// 返回页面数据
	private Response response = new Response();

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}
	
	/**
	 * 1.根据城市id获取所有启用的高校信息
	 * @return
	 */
	public String getSchoolsByCityId(){
		List<School> listSchools = schoolService.findSchoolsByCityId(cityId);
		if(listSchools != null && listSchools.size()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("获取高校列表成功");
			response.setData(listSchools);
		} else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("获取高校列表失败/列表为空");
			response.setData(listSchools);
		}
		return "findSchoolsByCityId";
	}
	
	/**
	 * 2.分页条件查询高校
	 * @return
	 */
	public String listPage(){
		String page = ServletActionContext.getRequest().getParameter("page");
		String rows = ServletActionContext.getRequest().getParameter("rows");
		String id = ServletActionContext.getRequest().getParameter("id");
		String name = ServletActionContext.getRequest().getParameter("name");
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");
		String provinceId = ServletActionContext.getRequest().getParameter("provinceId");
		String cityId = ServletActionContext.getRequest().getParameter("cityId");
		GridPageBean<School> listPage = schoolService.findByPage(page, rows, id, name, startDate, endDate, provinceId, cityId);
		if(listPage!=null && listPage.getRecords()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		response.setData(listPage);
		return "listPageSUCCESS";
	}
	
	/**
	 * 3.根据id查询高校信息
	 * @return
	 */
	public String findById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		School school = schoolService.findById(id);
		if(school!=null){
			response.setData(school);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		return "findByIdSUCCESS";
	}
	
	/**
	 * 4.保存或者更新高校信息
	 * @return
	 */
	public String saveSchool(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String name = ServletActionContext.getRequest().getParameter("name");
		String provinceId = ServletActionContext.getRequest().getParameter("provinceId");
		String cityId = ServletActionContext.getRequest().getParameter("cityId");
		try {
			schoolService.save(id,name,provinceId,cityId);
			response.setResult(Result.SUCCESS.toString());
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "saveSUCCESS";
	}
	
	/**
	 * 5.开启或者屏蔽高校信息
	 * @return
	 */
	public String updateStatusById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String status = ServletActionContext.getRequest().getParameter("status");
		try {
			schoolService.updateStatus(id,status);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("更新状态成功");
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			response.setMessage("更新状态失败");
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "updateStatusSUCCESS";
	}

}
